package com.tdam2011.grupo06.gui;

import android.app.NotificationManager;
import android.app.TabActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.TabHost;

import com.tdam2011.grupo06.R;
import com.tdam2011.grupo06.control.BobMessageService;

public class Main extends TabActivity {	
	Intent service;
	private BobMessageService mBoundService;
	private boolean mIsBound = false;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
		service = new Intent(this, com.tdam2011.grupo06.control.BobMessageService.class);
		startService(service);
		doBindService();
        SetupTabs();
    }
    
    void SetupTabs()
    {

    	TabHost host=getTabHost();
    	
    	TabHost.TabSpec spec=host.newTabSpec("tag1");
        Intent in1=new Intent(this, ListadoContactos.class);
        spec.setIndicator("" ,getResources().getDrawable(R.drawable.tab_contactos));
        spec.setContent(in1);
        
        TabHost.TabSpec spec2=host.newTabSpec("tag2");
        Intent in2=new Intent(this, ListadoMensajes.class);
        spec2.setIndicator("" ,getResources().getDrawable(R.drawable.tab_mensajes));
        spec2.setContent(in2);
        
        TabHost.TabSpec spec3=host.newTabSpec("tag3");
        Intent in3=new Intent(this, ListadoHistorial.class);
        spec3.setIndicator("" ,getResources().getDrawable(R.drawable.tab_historial));
        spec3.setContent(in3);
        
        TabHost.TabSpec spec4=host.newTabSpec("tag4");
        Intent in4=new Intent(this, ListadoConectividad.class);
        spec4.setIndicator("" ,getResources().getDrawable(R.drawable.tab_conectividad));
        spec4.setContent(in4);
        
        host.addTab(spec);
        host.addTab(spec2);
        host.addTab(spec3);
        host.addTab(spec4);
        
        if(getIntent().getExtras()!=null){
        	((NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(1);
        	host.setCurrentTab(1);
        }
    }

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}
	
	@Override
	protected void onStart() {
		super.onStart();

	}
	
	private ServiceConnection mConnection = new ServiceConnection() {
	    public void onServiceConnected(ComponentName className, IBinder service) {
	        // This is called when the connection with the service has been
	        // established, giving us the service object we can use to
	        // interact with the service.  Because we have bound to a explicit
	        // service that we know is running in our own process, we can
	        // cast its IBinder to a concrete class and directly access it.
	        mBoundService = ((BobMessageService.LocalBinder)service).getService();
	        mIsBound = true;
	        mBoundService.clearMessages();
	    }

	    public void onServiceDisconnected(ComponentName className) {
	        // This is called when the connection with the service has been
	        // unexpectedly disconnected -- that is, its process crashed.
	        // Because it is running in our same process, we should never
	        // see this happen.
	        mBoundService = null;
	    }
	};

	void doBindService() {
	    // Establish a connection with the service.  We use an explicit
	    // class name because we want a specific service implementation that
	    // we know will be running in our own process (and thus won't be
	    // supporting component replacement by other applications).
		bindService(service, mConnection, Context.BIND_AUTO_CREATE);
	}

	void doUnbindService() {
	    if (mIsBound) {
	        // Detach our existing connection.
	    	unbindService(mConnection);
	        mIsBound = false;
	        mBoundService.stopSelf();
	    }
	}
}